import {
  Component,
  OnInit, ViewEncapsulation,
  OnDestroy
} from '@angular/core';
import {ActivatedRoute, Params, Router} from "@angular/router";
import {Subscription} from "rxjs";

import {ToolsService} from "@shared/service/tools/tools.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {FeedbackSevice} from "@shared/service/feed-back/FeedbackSevice";

import {SeoService} from "@core/seo/seo.service";
import {FeedService} from "@feed/feed.service";
import {FeedCommentRespVO, FeedRespVO} from "@interface/index";


@Component({
  selector: 'feed-detail',
  templateUrl: './feed-detail.component.html',
  styleUrls: ['./feed-detail.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class FeedDetailComponent implements OnInit, OnDestroy {
  public feed: FeedRespVO;
  public comments: Array<FeedCommentRespVO> = [];
  public stateStore = {};
  public params = {
    userId: '',
    feedId: 0
  };
  subscription: Subscription = new Subscription();

  constructor(public route: ActivatedRoute,
              public tools: ToolsService,
              public feedService: FeedService,
              public user: AuthService,
              public feedback: FeedbackSevice,
              private _seoService: SeoService,
              private router: Router) {
    this.params.userId = this.user.getUserId();
    this.route.params.subscribe((params: Params) => {
      this.params.feedId = params['contentId'];
    });
  }

  /**
   * @description 获取帖子详情
   * @author ouyqx691
   * @date 2017/12/14
   * @param feedId  帖子Id
   */
  getFeedDetail() {
    this.feedService.getFeedDetail(this.params.feedId).subscribe(res => {
      if (res.result === '0' && res.data) {
        this.feed = res.data;
        this._seoService.setTKD(
          `${res.data.title}_体育比赛资讯`,
          `${res.data.title}_${res.data.circleName}_${res.data.labels[0]['labelName']}`,
          `求苗体育聚集各类体育产业IP，包含但不限于体育赛事、体育培训、体育旅游、体育赞助、体育用品等各大板块，
      实现多种资源的聚合与互通, 赛事运营管理、合作机构对接、消费数据统计及线上推广等多服务轻松一键搞定的健康运动服务的互联网平台`
        );
      }
    });
  }

  /**
   * @description 获取评论
   * @author ouyqx691
   * @date 2018/1/25
   */
  getComments() {
    const params = {
      feedId: this.params.feedId,
      page: 1,
      rows: 3
    };
    this.feedService.getFeedArticleComment(params).subscribe(res => {
      if (res.result === '0' && res.data) {
        this.comments = res.data.list;
      }
    });
  }

  /**
   * @description 处理点赞
   * @author ouyqx691
   * @date 2018/1/25
   */
  handleDigg() {
    if (!this.user.hasLoggedIn() && !this.user.getUserId()) {
      this.tools.showToast('登录之后才可进行此操作', 1000);
      return;
    }
    if (!this.feed.isDigg) {
      this.digg();
    }
    // else {
    //   this.cancelDigg();
    // }
  }

  /**
   * @description 点赞
   * @author ouyqx691
   * @date 2018/1/25
   */
  digg() {
    this.feedService.digg(this.params).subscribe((res) => {
      if (res.result === '0') {
        this.feed.diggCount++;
        this.feed.isDigg = true;
        this.getFeedDetail();
        this.tools.showToast('点赞成功', 1000);
      }
    });
  }

  /**
   * @description 取消点赞
   * @author ouyqx691
   * @date 2018/1/25
   */
  cancelDigg() {
    this.feedService.cancelDigg(this.params).subscribe((res) => {
      if (res.result === '0') {
        this.feed.diggCount--;
        this.feed.isDigg = false;
        this.tools.showToast('取消成功', 1000);
      }
    });
  }

  /**
   * @description 处理关注
   * @author ouyqx691
   * @date 2018/1/25
   */
  handleFollow() {
    if (!this.user.hasLoggedIn() && !this.user.getUserId()) {
      this.tools.showToast('登录之后才可进行此操作', 1000);
      return;
    }
    if (this.feed.isFollow === 0) { // 未关注
      this.feedback.GetFeedFierd(this.feed.userId).subscribe((res) => {
        if (res.result === '0') {
          this.feed.isFollow = 1;
          this.tools.showToast(res.msg, 1000);
        }
      });
    } else {
      this.feedback.GetFeedFierd(this.feed.userId, false).subscribe((res) => {
        if (res.result === '0') {
          this.feed.isFollow = 0;
          this.tools.showToast(res.msg, 1000);
        }
      });
    }
  }

  /**
   * @description 前往目标页面
   * @author zhupw1360
   * @date 2017/12/14
   * @param circleId  圈子ID
   */
  goToCircleDetail(circleId: number) {
    this.router.navigate([`/community/circle`, circleId]);
  }

  ngOnInit() {
    this.getFeedDetail();
    this.getComments();
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}
